package com.liugl.activitytest

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.widget.Toast
import kotlinx.android.synthetic.main.first_layout.*
import utils.L

class FirstActivity : BasicActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.first_layout)

        L.d("F TASK ID $taskId")

        button0.setOnClickListener {
            val intent = Intent(this, FirstActivity::class.java)
            startActivity(intent)
        }

        //跳转到SecondActivity
        button1.setOnClickListener {
//            val intent = Intent("com.liugl.activitytest.ACTION_START")
//            val intent = Intent(this, SecondActivity::class.java)
//            intent.addCategory("com.liugl.activitytest.MY_CATEGORY")
//            intent.putExtra("data", "Hello Second")
//            startActivityForResult(intent, 1)
//            startActivity(intent)

            SecondActivity.actionStart(this, "DATA1", "DATA2")
        }

        //打开浏览器
        open_browser.setOnClickListener {
            val intent = Intent(Intent.ACTION_VIEW)
            intent.data = Uri.parse("https://www.baidu.com")
            startActivity(intent)
        }

        //打开拨号盘并输入10086
        tel.setOnClickListener {
            val intent = Intent(Intent.ACTION_DIAL)
            intent.data = Uri.parse("tel:10086")
            startActivity(intent)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        when (requestCode) {
            1 -> if (resultCode == RESULT_OK) {
                Toast.makeText(this, data?.getStringExtra("data_return"), Toast.LENGTH_SHORT).show()
            }
        }
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.main, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            R.id.add_item -> Toast.makeText(this, "ADD", Toast.LENGTH_SHORT).show()
            R.id.remove_item -> Toast.makeText(this, "REMOVE", Toast.LENGTH_SHORT).show()
        }
        return true
    }

    override fun onRestart() {
        super.onRestart()
        L.d("FirstActivity restart")
    }
}